package com.mentaleye.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.mentaleye.bean.DormitoryStudentBean;
import com.mentaleye.bean.OaClassBean;
import com.mentaleye.bean.OaClassRelationBean;
import com.mentaleye.bean.StudentsBean;
import com.mentaleye.biz.DormitoryBiz;
import com.mentaleye.biz.DormitoryStudentBiz;
import com.mentaleye.biz.OaClassBiz;
import com.mentaleye.biz.OaClassRelationBiz;
import com.mentaleye.biz.StudentsBiz;
import com.mentaleye.dal.model.OaClassRelationExample;
import com.mentaleye.enums.CommonStatus;
import com.mentaleye.enums.DictionaryEnums;
import com.mentaleye.util.CommonPage;

@Controller
@RequestMapping("/dormitoryStudent")
public class DormitoryStudentController {
	@Autowired private DormitoryStudentBiz dormitoryStudentBiz;
	@Autowired private OaClassBiz oaClassBiz;
	@Autowired private StudentsBiz studentBiz;
	@Autowired private DormitoryBiz dormitoryBiz;
	@Autowired private OaClassRelationBiz oaClassStudentBiz;
	
	@RequestMapping("/List")
	public ModelAndView list() {
		ModelAndView view = new ModelAndView("lm/dormitory/dormitoryStudentList");
		view.addObject("enumMap", JSONObject.toJSONString(CommonStatus.enumMap));
		return view;
	}
	//获得宿舍列表数据
	@RequestMapping("/ListData")
	@ResponseBody
	public String dormitoryListData(CommonPage commonPage) {
		dormitoryStudentBiz.queryDormitoryByPage(commonPage);
		System.out.println(commonPage.toSHowPage());
		return commonPage.toSHowPage();
	}
	
	@RequestMapping("/editDormitoryStudent")
	public ModelAndView editDormitoryStudent(@RequestParam(value = "stuId", required = false) Integer stuId,@RequestParam(value = "dorId", required = false) Integer dorId){
		ModelAndView view = new ModelAndView("lm/dormitory/dormitoryStudentEdit");
		view.addObject("oaClassList", oaClassBiz.getAllOaClass());
		view.addObject("studentList", studentBiz.getList());
		view.addObject("dormitoryList",dormitoryBiz.queryDormitoryWithRest());
		
		return view;
	}
	
	@RequestMapping("/editDormitoryStudentInfo")
	public ModelAndView editDormitoryStudentInfo(@RequestParam(value = "stuId", required = false) Integer stuId,@RequestParam(value = "dorId", required = false) Integer dorId,@RequestParam(value = "stayStatus", required = false) Integer stayStatus){
		OaClassRelationExample example = new OaClassRelationExample();
		example.createCriteria().andPersonIdEqualTo(stuId).andPersonTypeEqualTo(DictionaryEnums.STUDENT_TYPE.getCode());//对应字典中的学生字段。此处是判断班级关联学生
		ModelAndView view = new ModelAndView("lm/dormitory/dormitoryStudentEditInfo");
		OaClassBean oaClassBean = new OaClassBean();
		List<OaClassRelationBean> oaClassRelation = oaClassStudentBiz.queryOaclassRelation(example);
		if(oaClassRelation !=null && !oaClassRelation.isEmpty()){
			for (OaClassRelationBean oaClassRelationBean : oaClassRelation) {
				oaClassBean.setOaclassId(oaClassRelationBean.getOaclassId());
				view.addObject("oaCls", oaClassBiz.queryOaclassData(oaClassBean));
			}
		}else{
			view.addObject("oaCls", null);
		}
		view.addObject("dorId", dorId);
		view.addObject("stuId", stuId);
		view.addObject("student", studentBiz.getStudentById(stuId));
		view.addObject("notCheckIn", CommonStatus.NOT_CHECKIN.getCode());
		view.addObject("hasCheckIn", CommonStatus.HAS_CHECK_IN.getCode());
		view.addObject("hasMoveOut", CommonStatus.HAS_MOVE_OUT.getCode());
		//view.addObject("dormitoryStudent", dormitoryStudentBiz.queryDormitoryRelateStudentByStuId(stuId));
		view.addObject("dormitoryStudent", dormitoryStudentBiz.queryDorStudentByStuIdAndDor(stuId,dorId,stayStatus));
		return view;
	}
	
	@RequestMapping("/studentRemove")
	public ModelAndView studentRemove(@RequestParam(value = "stuId", required = false) Integer stuId,@RequestParam(value = "dorId", required = false) Integer dorId){
		ModelAndView view = new ModelAndView("lm/dormitory/dormitoryStudentRemove");
		view.addObject("stuId", stuId);
		view.addObject("dorId", dorId);
		view.addObject("stayStatus", CommonStatus.HAS_MOVE_OUT.getCode());
		view.addObject("dormitoryStudent", dormitoryStudentBiz.queryDormitoryRelateStudentByStuId(stuId));
		return view;
	}
	@RequestMapping("getStudentByClass")
	@ResponseBody
	public String getStudentByClass(@RequestParam(value = "oaclassId", required = false) Integer oaclassId){
		List<OaClassRelationBean> OaClassRelationBean = oaClassStudentBiz.queryStudentByClassId(oaclassId);
		List<Integer> ids = new ArrayList<Integer>();
		if(OaClassRelationBean!=null){
			for (OaClassRelationBean oaClassRelationBean : OaClassRelationBean) {
				ids.add(oaClassRelationBean.getPersonId());
			}
			List<StudentsBean> studentsBean = studentBiz.getListBeanByIds(ids);
			return JSONObject.toJSONString(studentsBean);
		}else{
			return null;
		}
		
	}
	@RequestMapping("/save")
	public @ResponseBody String save(DormitoryStudentBean dormitoryStudentBean) {
		return JSONObject.toJSONString(dormitoryStudentBiz.editDormitoryStudent(dormitoryStudentBean));
	}

	@RequestMapping("/delete")
	public @ResponseBody String delete(@RequestParam(value = "stuId", required = false) Integer stuId,@RequestParam(value = "dorId", required = false) Integer dorId) {
		return JSONObject.toJSONString(dormitoryStudentBiz.removeDormitoryStudent(stuId,dorId));
	}
	@RequestMapping("/deleteStatus")
	public @ResponseBody String deleteStatus(@RequestParam(value = "stuId", required = false) Integer stuId) {
		return JSONObject.toJSONString(dormitoryStudentBiz.removeDormitoryStudentByStatus(stuId,CommonStatus.HAS_MOVE_OUT.getCode()));
	}
}
